define(['zepto', 'wx', 'sui'], function($, wx) {
  return {
    init: function(data) {
      var that = this;
      that.initForm(data.stars, data.roundId);
      that.shareMenu(data.jsConfig, data.stars, data.roundId);
    },
    initForm: function(stars, roundId) {
      
      $(document).on("click", '#NeedShare', function(e) {
        e.preventDefault();
        $.alert('分享到朋友圈后可以查看完整报告');
      })
      
      var starIndex = 0;
      var starAnimate = setInterval(function(){
        if(starIndex < stars) {
          $('.modal .star-group').children().eq(starIndex).addClass("active");
          starIndex ++;
        }else {
          clearInterval(starAnimate);
        }
      }, 500)
    },
    
    shareMenu: function(config, stars, roundId) {
      var that = this;
      var isDebug = location.search.indexOf('debug=true') > 0
      wx.config({
        debug: isDebug, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
        appId: config.appid,
        timestamp: config.timestamp,
        nonceStr: config.nonce_str,
        signature: config.signature,
        jsApiList: ['checkJsApi',
                    'onMenuShareTimeline',
                    'onMenuShareAppMessage',
                    'onMenuShareQQ',
                    'onMenuShareWeibo']
      });
      
      wx.ready(function(){
        
        var shareJson = {
            title: "陆老师小课堂-口算训练",
            desc:   "娃在20以内的口算训练中得到" + stars + "星。再接再励、永创佳绩。",
            link:   window.__CTX__.domain,
            imgUrl: window.__CTX__.domain + "/resource/images/edooschool.png"
        };
        
        wx.checkJsApi({
          jsApiList: ['onMenuShareTimeline',
                      'onMenuShareAppMessage',
                      'onMenuShareQQ',
                      'onMenuShareWeibo']
        });
        
        wx.onMenuShareAppMessage(
          $.extend({}, shareJson, {"success": 
            function() {
              window.__CTX__.share(window.__CTX__.root + "/oral-share", {
                "current": window.location.href,
                "marks": "口算完成分享",
                "type": "朋友"
              })
            }
          })
        )
        wx.onMenuShareQQ(
          $.extend({}, shareJson, {"success": 
            function() {
              window.__CTX__.share(window.__CTX__.root + "/oral-share", {
                "current": window.location.href,
                "marks": "口算完成分享",
                "type": "QQ"
              })
            }
          })
        )
        wx.onMenuShareWeibo(
          $.extend({}, shareJson, {"success": 
            function() {
              window.__CTX__.share(window.__CTX__.root + "/oral-share", {
                "current": window.location.href,
                "marks": "口算完成分享",
                "type": "微博"
              })
            }
          })
        )
        
        wx.onMenuShareTimeline({
          title : shareJson.title + "," + shareJson.desc,
          desc: shareJson.desc,
          link:   shareJson.link,
          imgUrl:   shareJson.imgUrl,
          success: function() {
            window.__CTX__.share(window.__CTX__.root + "/oral-share", {
              "current": window.location.href,
              "marks": "口算完成分享",
              "type": "朋友圈",
              "round": roundId
            }, function(){
              window.location.href = $('#NeedShare').data("link");
            })
          }
        });
      });
    }
  }
})


